package cn.school.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.school.system.domain.BiGood;
import cn.school.system.domain.vo.RandPaperParaVo;
import cn.school.system.service.IBiExtractPaperService;
import cn.school.system.service.IBiGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.school.system.mapper.BiPaperMapper;
import cn.school.system.domain.BiPaper;
import cn.school.system.service.IBiPaperService;

/**
 * 纸条信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-15
 */
@Service
public class BiPaperServiceImpl implements IBiPaperService
{
    @Autowired
    private BiPaperMapper biPaperMapper;
    @Autowired
    private IBiGoodService biGoodService;

    /**
     * 查询纸条信息
     *
     * @param id 纸条信息主键
     * @return 纸条信息
     */
    @Override
    public BiPaper selectBiPaperById(Long id)
    {
        return biPaperMapper.selectBiPaperById(id);
    }

    /**
     * 查询纸条信息列表
     *
     * @param biPaper 纸条信息
     * @return 纸条信息
     */
    @Override
    public List<BiPaper> selectBiPaperList(BiPaper biPaper)
    {
        return biPaperMapper.selectBiPaperList(biPaper);
    }

    /**
     * 新增纸条信息
     *
     * @param biPaper 纸条信息
     * @return 结果
     */
    @Override
    public int insertBiPaper(BiPaper biPaper)
    {
        return biPaperMapper.insertBiPaper(biPaper);
    }

    /**
     * 修改纸条信息
     *
     * @param biPaper 纸条信息
     * @return 结果
     */
    @Override
    public int updateBiPaper(BiPaper biPaper)
    {
        return biPaperMapper.updateBiPaper(biPaper);
    }

    /**
     * 批量删除纸条信息
     *
     * @param ids 需要删除的纸条信息主键
     * @return 结果
     */
    @Override
    public int deleteBiPaperByIds(Long[] ids)
    {
        return biPaperMapper.deleteBiPaperByIds(ids);
    }

    /**
     * 删除纸条信息信息
     *
     * @param id 纸条信息主键
     * @return 结果
     */
    @Override
    public int deleteBiPaperById(Long id)
    {
        return biPaperMapper.deleteBiPaperById(id);
    }

    @Override
    public Long findByOpneidCount(String openid) {
        return biPaperMapper.findByOpneidCount(openid);
    }

    @Override
    public List<Long> extractPaper(String openid,Long goodId, Long schoolId, Long paperId) {
        List<Long> list = new ArrayList<>();
        RandPaperParaVo vo = new RandPaperParaVo();
        if(goodId!=null) {
            BiGood good = biGoodService.selectBiGoodById(goodId);
            if (good.getGroupTag().equals("female")) {
                //女
                vo.setSex(2);
            } else {
                //男
                vo.setSex(1);
            }

            //需要抽几张
            vo.setNum(good.getPaperNum());
        }else if(paperId!=null){
            BiPaper biPaper = biPaperMapper.selectBiPaperById(paperId);
            vo.setSex(biPaper.getGender());
            vo.setNum(1);
        }

        vo.setOpenid(openid);
        vo.setSchoolId(schoolId);

        //抽取逻辑
        List<Long> cqFirstList = biPaperMapper.randPaperList(vo);
        if(cqFirstList!=null && cqFirstList.size()>0){
            list.addAll(cqFirstList);
        }

        //如果没有符合学校逻辑的纸条，获取符合学校逻辑的纸条不存在
        if(list==null || list.size() < vo.getNum()){
            //重新再抽一次
            vo.setSchoolId(null);
            vo.setPcSchoolId(schoolId);

            List<Long> cqSecondList = biPaperMapper.randPaperList(vo);
            if(cqSecondList!=null && cqSecondList.size()>0){
                list.addAll(cqSecondList);
            }
        }

        //增加纸条被抽取次数
        biPaperMapper.addReadNum(list);

        return list;
    }
}
